package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.EaseUI;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.fragment.huanxinUi.ChatFragment;

public class ChatActivity extends BaseActivity {
    public static ChatActivity activityInstance;
    private ChatFragment chatFragment;
    public String toChatUsername;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_chat;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        activityInstance = this;
        //get user id or group id
        int type = getIntent().getExtras().getInt("type");
        if (type == 1) {
            toChatUsername = getIntent().getExtras().getString("userId");
        } else {
            toChatUsername = getIntent().getExtras().getString(EaseConstant.EXTRA_USER_ID);
        }
        chatFragment = new ChatFragment();
        chatFragment.setMe(me);
        chatFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
        EMOptions options = new EMOptions();
        EaseUI.getInstance().init(getApplicationContext(), options);
    }


    @Override
    protected void onNewIntent(Intent intent) {
        String username = intent.getStringExtra("userId");
        if (toChatUsername.equals(username)) {
            super.onNewIntent(intent);
        } else {
            finish();
            startActivity(intent);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityInstance = null;
    }

    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
    }

    public String getToChatUsername() {
        return toChatUsername;
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    public static void start(Context me, String userId, int type) {
        Bundle bundle = new Bundle();
        bundle.putString("userId", userId);
        bundle.putInt("type", type);
        Intent intent = new Intent(me, ChatActivity.class);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }
}
